package com.loong.blog.mapper;

import com.loong.blog.model.map.ArticleAndDetailMap;
import com.loong.blog.model.po.ArticleDetailPO;
import com.loong.blog.model.po.ArticlePO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author chenglong
 * @date 2025/5/9 15:51
 */
public interface ArticleMapper {

    /**
     * 查询首页的文章列表
     * 1.阅读数量不能少于规定数量
     * 2.阅读数量从高到低排序
     * 3.取阅读数量排名前n个
     *
     * @author chenglong
     * @date 2025/5/9 15:55
     */
    @Select("""
        select id, user_id, title, summary, read_count, create_time, update_time
        from article
        where read_count >= #{lowRead}
        order by read_count desc
        limit #{topRead}
    """)
    @Results(id = "ArticleBaseMap", value = {
      @Result(id = true, column = "id", property = "id"),
      @Result(column = "user_id", property = "userId"),
      @Result(column = "title", property = "title"),
      @Result(column = "summary", property = "summary"),
      @Result(column = "read_count", property = "readCount"),
      @Result(column = "create_time", property = "createTime"),
      @Result(column = "update_time", property = "updateTime")
    })
    List<ArticlePO> topSortByReadCount(Integer lowRead, Integer topRead);

    /**
     * 添加文章
     *
     * @author loong
     * @date 2025/5/10 9:22
     */
    @Insert("""
    insert into article(user_id, title, summary, read_count, create_time, update_time)
    values (#{userId}, #{title}, #{summary}, #{readCount}, #{createTime}, #{updateTime})
    """)
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    int insertArticle(ArticlePO articlePO);

    /**
     * 添加文章详情
     *
     * @author loong
     * @date 2025/5/10 9:22
     */
    @Insert("""
        insert into article_detail(article_id, content)
        values (#{articleId}, #{content})
    """)
    int insertArticleDetail(ArticleDetailPO articleDetailPO);

    /**
     * 查询文章和文章详情
     *
     * @author loong
     * @date 2025/5/10 9:22
     */
    @Select("""
                    select m.id as article_id, title, m.summary, ad.content
                    from article m left join article_detail ad 
                             on m.id = ad.article_id
                    where m.id=#{id}
    """)
    @Results(
            id = "ArticleAndDetailMapper",
            value = {
                    @Result(id = true, column = "article_id", property = "id"),
                    @Result(column = "title", property = "title"),
                    @Result(column = "summary", property = "summary"),
                    @Result(column = "content", property = "content")
            }
    )
    ArticleAndDetailMap selectArticleAndDetail(Integer id);

    @Update("""
        update article set title = #{title}, summary = #{summary},
        update_time = #{updateTime}
        where id = #{id}
    """)
    int updateArticle(ArticlePO articlePO);

    @Update("""
        update article_detail set content=#{content} where article_id = #{articleId}
    """)
    int updateArticleDetail(ArticleDetailPO articleDetailPO);

    @Delete("""
        <script>
        delete from article where id in
        <foreach collection="list" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        </script>
    """)
    int deleteArticle(List<Integer> idList);

    @Delete("""
        <script>
        delete from article_detail where article_id in
        <foreach collection="list" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        </script>
    """)
    int deleteArticleDetail(List<Integer> idList);

    @Select("""
        select id, article_id, content from article_detail where article_id = #{id}
    """)
    @Results(id = "ArticleDetailMapper", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "article_id", property = "articleId"),
            @Result(column = "content", property = "content")
    })
    ArticleDetailPO selectArticleDetailByArticleId(Integer id);
}
